<?php
/*
* @User: 小灰灰 <762229008@qq.com>
* @Date: 2025-05-19
* @Time: 11:51:41
* @Info:
* Copyright (c) 2022~2025 https://xiaohuihui.cc All rights reserved.
*/

namespace app\api\controller;

use app\admin\model\user\User as UserModel;
use think\exception\ValidateException;
use think\facade\Cache;

class User extends Base
{

    protected $noNeedLogin = ["login", "register", "logout"];

    public function index()
    {
        $this->success('hello, welcome to User api');
    }

    // 登录
    public function login()
    {
        $account  = $this->request->post("account");
        $password = $this->request->post("password");
        $rule     = [
            'account|账户'  => 'require|length:3,30',
            'password|密码' => 'require|length:3,30',

        ];
        $data     = [
            'account'  => $account,
            'password' => $password,

        ];
        try {
            $this->validate($data, $rule);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            $this->error($e->getError());
        }
        $userModel = new UserModel();
        try {
            $res = $userModel->login($account, $password);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success($res);
    }

    // 注册
    public function register()
    {

    }

    // 退出登录
    public function logout()
    {
        Cache::delete($this->token);
        $this->success('退出成功');
    }

}
